#!/bin/sh
#
# 分析系列与机型日志
#


#参数文件, 需要分析的日志列表，每行一个
#paramsfile=$1
dir=$1
# 需要显示的top num
limit_num=$2

TMP_FILE="temp.log"
SORT_FILE="sort.log"

function analyse_phone() {
	logdir=$1
	for filename in `ls $logdir`
	do
		awk '$7~"list-" {print $7}' $logdir"/"$filename | awk 'match($0, /select_id=[0-9]+/) {print substr($0, RSTART+10, RLENGTH-10)}' >> $TMP_FILE
	done
}

function analyse_series() {
	logdir=$1
	for filename in `ls $logdir`
	do
		awk '$7~"list-" {print $7}' $logdir"/"$filename | awk 'match($0, /series_id=[0-9]+/) {print substr($0, RSTART+10, RLENGTH-10)}' >> $TMP_FILE
	done
}

function usort() {
	tempfile=$1
	sort $tempfile | uniq -c | sort -n >> $SORT_FILE
}

cat /dev/null > $TMP_FILE
cat /dev/null > $SORT_FILE

analyse_phone $dir

usort $TMP_FILE $limit_num

tail -n $limit_num $SORT_FILE
